package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mongo.SundayProductTag;
import org.itboys.core.service.mongo.web.SundayProductTagService;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/22
 * Description:
 */

@Controller
@RequestMapping("/sunday/web/manageTag")
public class SundayProductTagController extends BaseController {

    @Autowired
    private SundayProductTagService labelService;

    /**
     * 首页
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        return "/sunday/product/tag/index";
    }

    /**
     * 分页查询标签
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value = "productId", required = true) Long productId
    ) {
        try {
            PageResult<SundayProductTag> result = labelService.selectTags(request, productId);
            JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "id", required = true) Long id
    ) {
        try {

            SundayProductTag label = labelService.findOne(id);
            AjaxUtils.renderJson(response, label);
        } catch (Exception e) {
            e.printStackTrace();

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/manageTag/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或修改商品标签
     *
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value = "id", required = true) Long id,
                     @RequestParam(value = "productId", required = true) Long productId,
                     @RequestParam(value = "labelId", required = true) Long labelId,
                     @RequestParam(value = "productName", required = true) String productName,
                     @RequestParam(value = "labelName", required = true) String labelName

    ) {
        try {
            SundayProductTag label = new SundayProductTag();
            label.setProductId(productId);
            label.setLabelId(labelId);
            label.setProductName(productName);
            label.setLabelName(labelName);

            labelService.save(id, label);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/manageTag/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
            ;
        }
    }

    /**
     * 逐个删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            labelService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/manageTag/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 标签选择器
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          @RequestParam(value = "chooseId",required = false)String chooseId,//选择器id（当页面存在多个选择器时使用该字段区分）

                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        model.addAttribute("chooseId",chooseId);
        if(isSingle==null){
            return "/sunday/product/tag/choose/choose_erro";
        }else if(isSingle==1){
            return "/sunday/product/tag/choose/choose_single";
        }else if(isSingle==0){
            return "/sunday/product/tag/choose/choose_muilt";
        }

        return "/finance/product/choose/choose_erro";
    }

}
